package com.jf.identity.service.impl;

import com.jf.identity.dao.dc.EmergencyCodeDao;
import com.jf.identity.dao.vis.CodeDao;
import com.jf.identity.dao.visexamid.EmergencyFeedBackDao;
import com.jf.identity.entity.*;
import com.jf.identity.holder.ExamBaseParamHolder;
import com.jf.identity.service.EmergencyCodeService;
import com.jf.identity.util.PostPictureUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by x on 2018/4/13.
 */
@Service
public class EmergencyCodeServiceImpl implements EmergencyCodeService {
    private static final String EMERGENCY_REPORT = "3";
    @Resource
    private EmergencyCodeDao emergencyCodeDao;
    @Resource
    private ExamBaseParamHolder examBaseParamHolder;
    @Resource
    private EmergencyFeedBackDao emergencyFeedBackDao;
    @Resource
    private CodeDao codeDao;
    @Resource
    private ExamBaseParamValidator examBaseParamValidator;
    @Override
    public List<EmergencyCode> getEmergencyCods(ExamBaseParam examBaseParam) {
//        ExamBaseParam examBaseParam = examBaseParamHolder.getExamBaseParam();
        ExamBaseParam.isBlank(examBaseParam);
        //对考场的机构代码、考试计划编号做验证
        examBaseParamValidator.validate(examBaseParam, ExamBaseParam.EXAM_ID, ExamBaseParam.ORGCODE);
        List<EmergencyCode> emergencyCodes = emergencyCodeDao.getEmergencyCodes();
        //应急预案类型为 9
        List<Code> codes = codeDao.getCodeByType("9");
        for (EmergencyCode emergencyCode : emergencyCodes) {
            for (Code code : codes) {
                if (emergencyCode.getTypeId().equals(code.getUid())) {
                    emergencyCode.setType(code.getName());
                }
            }
        }
        return emergencyCodes;
    }

    @Override
    public boolean insertEmergencyFeedBackInfo(EmergencyFeedBackInfo emergencyFeedBackInfo) {
        ExamBaseParam examBaseParam = ExamBaseParam.copyOf(emergencyFeedBackInfo);
        ExamBaseParam.isBlank(examBaseParam);
        //对考场的机构代码、考试计划编号、考试场次编号、逻辑考场号做验证
        examBaseParamValidator.validate(examBaseParam,ExamBaseParam.EXAM_ID, ExamBaseParam.EXAM_SESSION_NUM,
                ExamBaseParam.LOGIC_EXAM_ROOM_NUM, ExamBaseParam.ORGCODE);
        String uid = emergencyFeedBackInfo.getEmergencyReportId();
        uid = uid.replaceAll("-", "");
        emergencyFeedBackInfo.setEmergencyReportId(uid);
        //针对获取的每张图片用“;”进行拼接，拼接为一个imageUrls，然后传入数据库
        //对传入的信息进行入库，如果上传信息包含图片，则按以下步骤处理图片入库问题
        /*
         * ① 将头像存入本地，并获得待入库的存储路径
         * ② 取出数据库中保存的图片路径
         * ③ 将待入库的图片路径，写入数据库中
         * ④ 如果写入数据库成功则将第二步取出的图片路径(前提是存在)进行删除文件操作
         */
        List<String> imageList = emergencyFeedBackInfo.getImages();
        String imageUrls;
        StringBuilder imageUrlsSB = new StringBuilder();
        if (!StringUtils.isEmpty(imageList)) {
            for (String s : imageList) {
                if (!StringUtils.isEmpty(s)) {
                    String pic = PostPictureUtil.postPicAtLocal(s, "6", examBaseParam.getExamId());
                    imageUrlsSB.append(pic).append(";");
                }
            }
        }
        if (StringUtils.isEmpty(imageUrlsSB)) {
            imageUrls = "";
        } else {
            imageUrls = imageUrlsSB.toString();
        }
        //①
        emergencyFeedBackInfo.setImageUrls(imageUrls);
        //②
        String picOldPath = emergencyFeedBackDao.getImageUrlsByUid(examBaseParam,emergencyFeedBackInfo.getEmergencyReportId());
        //③
        int n = emergencyFeedBackDao.insertEmergencyFeedBackInfo(examBaseParam, emergencyFeedBackInfo);
        //④
        if (n > 0 && !StringUtils.isEmpty(picOldPath)) {
            //先要解析字符串，然后执行删除操作
            String[] images = picOldPath.split(";");
            String drive = PostPictureUtil.drive;
            for (String s : images) {
                PostPictureUtil.deleteLocalPic(drive + s);
            }
        }
        return n != 0;
    }

    @Override
    public boolean updateEmergencyReportStatus(EmergencyFeedBackInfo emergencyFeedBackInfo) {
        ExamBaseParam examBaseParam = ExamBaseParam.copyOf(emergencyFeedBackInfo);
        ExamBaseParam.isBlank(examBaseParam);
        //对考场的机构代码、考试计划编号、考试场次编号、逻辑考场号做验证
        examBaseParamValidator.validate(examBaseParam,ExamBaseParam.EXAM_ID, ExamBaseParam.EXAM_SESSION_NUM,
                ExamBaseParam.LOGIC_EXAM_ROOM_NUM, ExamBaseParam.ORGCODE);
        String uid = emergencyFeedBackInfo.getEmergencyReportId();
        uid = uid.replaceAll("-", "");
        emergencyFeedBackInfo.setEmergencyReportId(uid);
        int n = emergencyFeedBackDao.updateEmergencyReportStatus(examBaseParam,emergencyFeedBackInfo.getEmergencyReportId());
        return n != 0;
    }
}
